package cn.iocoder.yudao.module.erp.service.business.money.moneyaccount;

import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount.vo.MoneyAccountPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyaccount.vo.MoneyAccountSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneyaccount.MoneyAccountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.business.money.moneyaccount.MoneyAccountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 资金账户 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class MoneyAccountServiceImpl implements MoneyAccountService {

    @Resource
    private MoneyAccountMapper moneyAccountMapper;

    @Override
    public Long createMoneyAccount(MoneyAccountSaveReqVO createReqVO) {
        // 插入
        MoneyAccountDO moneyAccount = BeanUtils.toBean(createReqVO, MoneyAccountDO.class);
        moneyAccountMapper.insert(moneyAccount);
        // 返回
        return moneyAccount.getId();
    }

    @Override
    public void updateMoneyAccount(MoneyAccountSaveReqVO updateReqVO) {
        // 校验存在
        validateMoneyAccountExists(updateReqVO.getId());
        // 更新
        MoneyAccountDO updateObj = BeanUtils.toBean(updateReqVO, MoneyAccountDO.class);
        moneyAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoneyAccount(Long id) {
        // 校验存在
        validateMoneyAccountExists(id);
        // 删除
        moneyAccountMapper.deleteById(id);
    }

    private void validateMoneyAccountExists(Long id) {
        if (moneyAccountMapper.selectById(id) == null) {
            throw exception(MONEY_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public MoneyAccountDO getMoneyAccount(Long id) {
        return moneyAccountMapper.selectById(id);
    }

    @Override
    public PageResult<MoneyAccountDO> getMoneyAccountPage(MoneyAccountPageReqVO pageReqVO) {
        return moneyAccountMapper.selectPage(pageReqVO);
    }

}